package starter

import (
	"fmt"
	"gitee.com/kordar/bucket"
	"gitee.com/kordar/goadmin/admin"
	starter2 "gitee.com/kordar/goadmin/starter"
	command_starter "github.com/kordar/command-starter"
	dig_starter "github.com/kordar/dig-starter"
	"github.com/kordar/gocfg"
	gocfgmodule "github.com/kordar/gocfg-load-module"
	goframework_goredis "github.com/kordar/goframework-goredis"
	goframework_gorm_mysql "github.com/kordar/goframework-gorm-mysql"
	"github.com/kordar/gorbac"
	gorbac_starter "github.com/kordar/gorbac-starter"
	goredis_starter "github.com/kordar/goredis-starter"
	mysql_starter "github.com/kordar/mysql-starter"
	"github.com/redis/go-redis/v9"
	"go.uber.org/dig"
	"gorm.io/gorm"
	"labelsys/dataset"
)

func Server() {

	gormLogLevel := gocfg.GetSystemValue("gorm_log_level")

	startConfig := []gocfgmodule.GoCfgModule{

		// 注册dig组件
		dig_starter.NewDigModule("ioc", nil),

		// 注册rbac组件
		gorbac_starter.NewRbacModule("gorbac", func(moduleName string, item map[string]interface{}) {
			dig_starter.ProvideE(func() (*gorbac.RbacService, error) {
				return gorbac_starter.GetRbacService(), nil
			})
		}, "mysql", "goredis"),

		// 注册mysql组件
		mysql_starter.NewMysqlModule("mysql", func(moduleName string, itemId string, item map[string]string) {
			name := fmt.Sprintf("%s.%s", moduleName, itemId)
			dig_starter.ProvideE(func() (*gorm.DB, error) {
				return goframework_gorm_mysql.GetMysqlDB(itemId), nil
			}, dig.Name(name))
		}, gormLogLevel),

		// 命令行启动
		command_starter.CommandModule{},

		// 注册redis组件
		goredis_starter.NewRedisModule("goredis", func(moduleName string, itemId string, item map[string]string) {
			name := fmt.Sprintf("%s.%s", moduleName, itemId)
			dig_starter.ProvideE(func() (redis.UniversalClient, error) {
				return goframework_goredis.GetRedisClient(itemId), nil
			}, dig.Name(name))
		}),

		// bucket组件初始化
		bucket.NewModule("sys_bucket"),

		// labelsys dataset组件初始化
		dataset.NewLabelsysDatasetModule(),

		// goadmin组件初始化
		admin.GoAdminModule{},

		// 上传组件
		//uploadstarter.UploadModule{},
		// 异步任务
		//async_task_starter.AsyncTaskStarter{},

		// redis注册中心
		//registry_starter.RegistryModule{},
	}

	requirements := []string{"ioc", "bucket"}
	dependency := []string{"ioc"}

	starter2.ServerWithStarter(startConfig, requirements, dependency)
}
